// pages/history/history.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    pic: "",
    image:"",
    text:"",
    // 设置开始的位置
    startX: 0,
    startY: 0,
  },

  Decoder:function(str){
    var res="";
    for(var i=0;i<str.length;i++){
      if(str[i]=='#'){
        res+='&';
      }else{
        res+=str[i];
      }
    }
    return res;
  },

  onLoad: function () {
    var that=this
    wx.request({
      url: app.globalData.urlHome + '/user/getUserCollect',
      data: {
        "openid":app.globalData.openid
      },
      header: {
        "accept": "*/*",
        // "content-type": "application/json",
        "Authorization": app.globalData.token
      },
      method: "GET",
      success(r) {
        var len = r.data.length;
        if(len==0){
          that.setData({
            image:"/resources/images/myPage/nocollect.png",
            text:"你还没有收藏任何物语噢，快去附近看看吧~",
          })
          return;
        }

        var tem = [];
        for (var i = 1; i <= len; i++) {
          var card = r.data[i-1];
          card.id = i;
          card.isTouchMove=false;
          tem.push(card);
        }

        that.setData({
          dataList: tem
        })
        console.log(that.data.dataList)
      }
    })
  },

  // 开始滑动
  touchStart(e) {
    let dataList = [...this.data.dataList]
    dataList.forEach(item => {
      if (item.isTouchMove) {
        item.isTouchMove = !item.isTouchMove;
      }
    });
    this.setData({
      dataList: dataList,
      startX: e.touches[0].clientX,
      startY: e.touches[0].clientY
    })
  },

  touchMove(e) {
    let moveX = e.changedTouches[0].clientX;
    let moveY = e.changedTouches[0].clientY;
    let indexs = e.currentTarget.dataset.index;
    let dataList = [...this.data.dataList]

    let angle = this.angle({
      X: this.data.startX,
      Y: this.data.startY
    }, {
      X: moveX,
      Y: moveY
    });

    dataList.forEach((item, index) => {
      item.isTouchMove = false;
      // 如果滑动的角度大于30° 则直接return；
      if (angle > 30) {
        return
      }

      if (indexs === index) {
        if (moveX > this.data.startX) { // 右滑
          item.isTouchMove = false;
        } else { // 左滑
          item.isTouchMove = true;
        }
      }
    })

    this.setData({
      dataList
    })
  },

  /**
   * 计算滑动角度
   * @param {Object} start 起点坐标
   * @param {Object} end 终点坐标
   */
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },

  // 删除
  delItem(e) {
    var that=this;
    
    let id = e.currentTarget.dataset.id;
    let dataList = [...this.data.dataList];
    console.log('id--->', id);

    wx.showModal({
      title: '提示',
      content: '确认删除此物语？',
      success:(res)=>{
        if (res.confirm) {
          for (let i = 0; i < dataList.length; i++) {
            const item = dataList[i];
            item.isTouchMove = false;
            if (item.id === id) {
              wx.request({
                url: app.globalData.urlHome+'/user/deleteCollect',
                header:{
                  "accept": "*/*",
                  "content-type": "application/json",
                  "x-auth-token":app.globalData.token
                },
          
                data:{
                 "openid":app.globalData.openid,
                 "cardId":item.cardid
                },
                method:'POST',
          
                success(r){
                  if(r.data=="delete collect success!"){
                    wx.showToast({
                      title: '删除成功！',
                    })
                  }
                  
                  var len=that.data.dataList.length;

                  if(len==0){
                    that.setData({
                      image:"/resources/images/myPage/kong.png",
                      text:"这里空空如也，去逛逛附近卡片吧！",
                    })
                  }
                }
              })
              dataList.splice(i, 1);
              break;
            }
          }
      
          this.setData({
            dataList
          })
        } else if (res.cancel) {
          return;
        }
      }
    })

    
  },

  Encoder: function (str) {
    var res = "";
    for (var i = 0; i < str.length; i++) {
      if (str[i] == '&') {
        res += '#';
      } else {
        res += str[i];
      }
    }
    return res;
  },

  onClick:function(e){
    var that = this;
    var index = e.currentTarget.dataset['index']
    console.log(index)
    wx.navigateTo({
      url: '../detail/detail?json=' + that.Encoder(JSON.stringify(this.data.dataList[index])),
    })
  }
})